package asapIs134CK.womi.client.games.logoGame.froggitTheFrog;

import asapIs134CK.womi.client.games.EditorWidget;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.editor.MapEditionWidget;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.editor.SelectionCellPanel;
import asapIs134CK.womi.client.games.logoGame.utils.exceptions.MissConstructMapException;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FroggitEditorWidget extends EditorWidget {
	//core game elements

	

	//GUI elements
	private SelectionCellPanel _availableCell;
	private Panel _editionTools;
	private DockPanel _frgEditorContainer;
	private Label _helpPanel;
	private MapEditionWidget _mapEditor;
	private Panel _sizeManager;
	
	//Static 
	//private static LogoGameImageBundle imgbundle = GWT.create(LogoGameImageBundle.class);
	
	public FroggitEditorWidget(){
		_frgEditorContainer = new DockPanel();
		_sizeManager = new VerticalPanel();
		_availableCell = new SelectionCellPanel();
		_helpPanel = new Label();
		_mapEditor = new MapEditionWidget(_availableCell.getDragCtrl());
		_editionTools = new VerticalPanel();
		initWidgetGUI();
		
		initWidget(_frgEditorContainer);
	}
	private void initWidgetGUI() {
		//Initialization in the following order : North, West, East, South, Center
		
		//North :
		Label editorTitle = new Label("Froggit Map Creator ! Welcome :-)");
		editorTitle.setStyleName("froggitTF_banner");
		_frgEditorContainer.add(editorTitle, DockPanel.NORTH);
		
		//West :
		initSizeManager();
		_frgEditorContainer.add(_sizeManager, DockPanel.WEST);
		
		//East :
		VerticalPanel eastPanel = new VerticalPanel();
		eastPanel.add(_availableCell);
		initEditionPanel();
		eastPanel.add(_editionTools);
		_frgEditorContainer.add(eastPanel, DockPanel.EAST);
		
		//South :
		initHelp();
		_frgEditorContainer.add(_helpPanel, DockPanel.SOUTH);
		
		//Center :
		_frgEditorContainer.add(_mapEditor, DockPanel.CENTER);
	}
	
	private void initEditionPanel() {
		Button startQuickTest = new Button("Quick test");
		Button saveIt = new Button("Save this maze");
		_editionTools.add(startQuickTest);
		_editionTools.add(saveIt);
		startQuickTest.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				FroggitWorld world = null;
				try {
					world = _mapEditor.getCorrespondingWorld();
					FroggitGameWigdet testGame = new FroggitGameWigdet(world);
					final DialogBox gamePopup = new DialogBox();
					VerticalPanel gameVertPanel = new VerticalPanel();
					
					gamePopup.setTitle("Test your new map here :)");
					
					gameVertPanel.add(testGame);
					
					Button closeGamePopupBut  = new Button("Back to Edition");
					closeGamePopupBut.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							gamePopup.hide();
							gamePopup.removeFromParent();
						}
					});
					gameVertPanel.add(closeGamePopupBut);
					gamePopup.add(gameVertPanel);
					gamePopup.center();
				} catch (MissConstructMapException e) {
					final DialogBox errDial = new DialogBox();
					errDial.setTitle("Oups !");
					VerticalPanel errVertPanel = new VerticalPanel();
					errVertPanel.add(new Label("Oups there is something wrong with this map :"));
					errVertPanel.add(new Label(e.getMsg()));
					Button closeBut = new Button("\"ok let me check that\"");
					closeBut.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							errDial.hide();
							errDial.removeFromParent();
						}
					});
					errVertPanel.add(closeBut);
					errDial.add(errVertPanel);
					errDial.center();
				}
				
			}
		});
		saveIt.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				final DialogBox saveDialog = new DialogBox();
				saveDialog.setTitle("Please fill in this form to submit your creation");
				FormPanel formContainer = new FormPanel();
				VerticalPanel formPanel = new VerticalPanel();
				formContainer.setWidget(formPanel);
				formPanel.add(new Label("Please Enter the following information about your Map"));
				
				//get the maze name
				Label nameLbl = new Label("Map's name");
				TextBox nameValue = new TextBox();
				nameValue.setText("My new so great map");
				formPanel.add(nameLbl);
				formPanel.add(nameValue);
				
				
				HorizontalPanel actionPanel = new HorizontalPanel();
				//put the validation button
				Button validation = new Button("Send my wonderful map");
				actionPanel.add(validation);
				//put a cancel Button
				Button cancel = new Button("Cancel");
				actionPanel.add(cancel);
				cancel.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						saveDialog.hide();
						saveDialog.removeFromParent();
					}
				});
				
				formPanel.add(actionPanel);
				
				saveDialog.setWidget(formContainer);
				saveDialog.center();
			}
		});
		
	}
	private void initSizeManager() {
		Label sizeMngTitle = new Label("Size Modification");
		_sizeManager.add(sizeMngTitle);
		Button addRow = new Button("add a new Row");
		Button addCol = new Button("add a new column");
		Button delRow = new Button("remove last Row");
		Button delCol = new Button("remove last column");
		_sizeManager.add(addRow);
		_sizeManager.add(addCol);
		_sizeManager.add(delRow);
		_sizeManager.add(delCol);
		
		addRow.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				_mapEditor.addARow();
			}
		});
		addCol.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				_mapEditor.addAColumn();
			}
		});
		delRow.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				_mapEditor.removeLastRow();
			}
		});
		delCol.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				_mapEditor.removeLastColumn();
			}
		});
		
	}
	
	private void initHelp() {
		_helpPanel.setText("put your mouse over something to get Help :-)");
	}
}
